<template>
  <div>
    <el-form :inline="true" :model="user" class="demo-form-inline">
      <el-form-item label="账号">
        <el-input v-model="user.username" placeholder="账号"></el-input>
      </el-form-item><br>
      <el-form-item label="密码">
        <el-input type="password" v-model="user.password" placeholder="密码"></el-input>
      </el-form-item><br>
      <el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
        <el-link :href=oauth2Url >oauth2登录</el-link>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return{
      oauth2Url:null,
      code:null,
      user:{}
    }
  },
  created() {
    this.axios.get("http://localhost:8080/system/user/getOauth2Url").then(
        res=>{
          console.log(res.data)
          this.oauth2Url =res.data
        }
    )
    if (this.$route.query.code) {
      this.code=this.$route.query.code
      this.axios.get("http://localhost:8080/system/user/auth",{params:{code:this.code}}).then(
          res=>{
            console.log(res)
            if (res.data.code == 200) {
              this.$message.success("登录成功")
              localStorage.setItem("token",res.data.data.token)
              this.$router.push("/house")
            }else{
              this.$message.error("登录失败")
            }
          }
      )
    }
  },
  methods:{
    login() {
      this.$router.push("/house")
    }
  }
}
</script>

<style scoped>

</style>
